//
//  YMCheckService.m
//  YMMedical
//
//  Created by y s on 2023/7/13.
//

#import "YMCheckService.h"
#import "YMNetwork.h"

@implementation YMCheckService

//上传 检测 图片
+ (void)requestUploadFileWithData:(NSData *)fileData
                         fileName:(NSString *)fileName
                         mimeType:(NSString *)mimeType
                          success:(void(^)(NSString *pathUrl, NSString *fileName))success
                          failure:(void(^)(NSString *message))failure{
    
    NSString *url = @"http://106.54.213.16/upload";
    [YMNetwork uploadDataWithUrl:url fileData:fileData fileName:fileName name:@"image" mimeType:mimeType parameters:@{} success:^(id  _Nonnull data) {
        if (data != nil && [data isKindOfClass:[NSDictionary class]]){
            if (success){
                NSString *path = [SDataUtil stringOf:data[@"img_file_path"] valid:@""];
                NSString *name = [SDataUtil stringOf:data[@"image_filename"] valid:@""];
                success(path,name);
            }
        }else{
            NSString *msg = [SDataUtil stringOf:data[@"message"] valid:@"上传失败"];
            if (failure){
                failure(msg);
            }
        }
    } failure:^(NSError * _Nonnull error) {
        if (failure){
            failure(@"上传失败失败");
        }
    }];
}
//采样 第一步 页面接口
+ (void)requestCheckSampleStepOneDataWithId:(NSString *)idStr
                                  stepFirst:(BOOL)stepFirst
                                    success:(void(^)(NSString *tipsStr, NSArray *arrImg))success
                                    failure:(void(^)(NSString *message))failure{
    NSString *url = [KTuoToUrl stringByAppendingString:@"/appsheets/sheetapi/getFormList"];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setValue:@"13aa4626c7014c1b93136c038019db8c" forKey:@"applicationid"];
    [dict setValue:@"68b6063091a54242a0d3b7794ef14529" forKey:@"formheaduuid"];
    [dict setValue:@0 forKey:@"needpage"];
    [dict setValue:@1 forKey:@"page"];
    [dict setValue:@20 forKey:@"pagesize"];
    
    NSMutableDictionary *dictField1 = [[NSMutableDictionary alloc] init];
    [dictField1 setValue:@"ktrpe1fzjzsdoek8" forKey:@"key"];
    [dictField1 setValue:[SDataUtil stringOf:idStr valid:@""] forKey:@"value"];
    
    NSMutableDictionary *dictField2 = [[NSMutableDictionary alloc] init];
    [dictField2 setValue:@"xkw6hzbgrureov83" forKey:@"key"];
    if ([YMLanguageUtil isEnglish]){
        if (stepFirst){
            [dictField2 setValue:@"9b736682-d5af-441c-9367-06e7a24639ef" forKey:@"value"];
        }else{
            [dictField2 setValue:@"0e6a8294-62bf-497c-8aee-ef984f797896" forKey:@"value"];
        }
    }else{
        if (stepFirst){
            [dictField2 setValue:@"168d2a84-8725-4a57-a54f-eab24433a466" forKey:@"value"];
        }else{
            [dictField2 setValue:@"a6d76715-3f36-44b9-bb1b-3328eb146b07" forKey:@"value"];
        }
    }
    NSMutableDictionary *dictField3 = [[NSMutableDictionary alloc] init];
    NSString *languageStr = [YMLanguageUtil isEnglish] ? @"69bd0b96-e704-4522-8543-4c140691793c":@"746f5cd0-dee7-4a10-8f91-75b27ac123b4";
    [dictField3 setValue:languageStr forKey:@"value"];
    [dictField3 setValue:@"vydvtklk0xjgsinm" forKey:@"key"];
    
    [dict setValue:@[dictField1,dictField2,dictField3] forKey:@"filters"];
    
    [YMNetwork postWithUrl:url parameters:dict success:^(id  _Nonnull data) {
        NSString *status = [SDataUtil stringOf:data[@"status"]];
        if ([YMNetwork tuoTuSuccess:status]){
            NSDictionary *dict = [SDataUtil dictOf:data[@"result"]];
            NSString *tipsStr = @"";
            NSArray *arrImg = [[NSArray alloc] init];
            if (dict != nil){
                NSArray *arrTemp = [SDataUtil arrayOf:dict[@"data"]];
                if (arrTemp.count > 0){
                    NSDictionary *dictTemp = [SDataUtil dictOf:arrTemp.firstObject];
                    if (dictTemp != nil){
                        tipsStr = [SDataUtil stringOf:dictTemp[@"ovoqe3wroqadzqso"] valid:@""];
                        arrImg = [SDataUtil arrayOf:dictTemp[@"9rjnk9bcnstxhdli"]];
                    }
                }
            }
            if (success){
                success(tipsStr,arrImg);
            }
        }else{
            NSString *msg = [SDataUtil stringOf:data[@"err_msg"] valid:KLanguage(@"net_error_later_retry")];
            if (failure){
                failure(msg);
            }
        }
    } failure:^(NSError * _Nonnull error) {
        if (failure){
            failure(KLanguage(@"net_error_later_retry"));
        }
    }];
}

//检测报告内容
+ (void)requestCheckReportDetailContentWithId:(NSString *)idStr
                                      success:(void(^)(NSString *contentText))success
                                      failure:(void(^)(NSString *message))failure{
    NSString *url = [KTuoToUrl stringByAppendingString:@"/appsheets/sheetapi/getFormList"];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setValue:@"13aa4626c7014c1b93136c038019db8c" forKey:@"applicationid"];
    [dict setValue:@"59159213b4734329b4b6f1e7a8756819" forKey:@"formheaduuid"];
    [dict setValue:@0 forKey:@"needpage"];
    [dict setValue:@1 forKey:@"page"];
    [dict setValue:@20 forKey:@"pagesize"];
    
    NSMutableDictionary *dictField = [[NSMutableDictionary alloc] init];
    [dictField setValue:@"euye5zvxoitubvte" forKey:@"key"];
    [dictField setValue:[SDataUtil stringOf:idStr valid:@""] forKey:@"value"];
    [dict setValue:@[dictField] forKey:@"filters"];
    
    [YMNetwork postWithUrl:url parameters:dict success:^(id  _Nonnull data) {
        NSString *status = [SDataUtil stringOf:data[@"status"]];
        if ([YMNetwork tuoTuSuccess:status]){
            NSDictionary *dict = [SDataUtil dictOf:data[@"result"]];
            NSString *contentStr = @"";
            if (dict != nil){
                NSArray *arrTemp = [SDataUtil arrayOf:dict[@"data"]];
                if (arrTemp.count > 0){
                    NSDictionary *dictTemp = [SDataUtil dictOf:arrTemp.firstObject];
                    if (dictTemp != nil){
                        contentStr = [SDataUtil stringOf:dictTemp[@"rzezhfttmviaah3v"] valid:@""];
                    }
                }
            }
            if (success){
                success(contentStr);
            }
        }else{
            NSString *msg = [SDataUtil stringOf:data[@"err_msg"] valid:KLanguage(@"net_error_later_retry")];
            if (failure){
                failure(msg);
            }
        }
    } failure:^(NSError * _Nonnull error) {
        if (failure){
            failure(KLanguage(@"net_error_later_retry"));
        }
    }];
}


//检测规则查询接口 idStr: 小类id
+ (void)requestCheckUserInforListBottomItemDataWithIdStr:(NSString *)idStr
                                                 success:(void(^)(NSString *nameStr,NSArray *arrItem))success
                                                 failure:(void(^)(NSString *message))failure{

    NSString *url = [KTuoToUrl stringByAppendingString:@"/appsheets/sheetapi/getFormList"];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setValue:@"13aa4626c7014c1b93136c038019db8c" forKey:@"applicationid"];
    [dict setValue:@"ded7b290da4641309118a69a18fc89b8" forKey:@"formheaduuid"];
    [dict setValue:@0 forKey:@"needpage"];
    [dict setValue:@1 forKey:@"page"];
    [dict setValue:@20 forKey:@"pagesize"];
    
    NSMutableDictionary *dictField = [[NSMutableDictionary alloc] init];
    [dictField setValue:@"tfilhnr91gkmekdd" forKey:@"key"];
    [dictField setValue:[SDataUtil stringOf:idStr valid:@""] forKey:@"value"];
    
    NSMutableDictionary *dictField1 = [[NSMutableDictionary alloc] init];
    NSString *languageStr = [YMLanguageUtil isEnglish] ? @"86249b3a-0a54-4602-9d49-d8deafc850a6":@"7599b8c0-e0d2-45d2-aaee-ba9de5a349be";
    [dictField1 setValue:@"yllmwtdxzftugo1h" forKey:@"key"];
    [dictField1 setValue:languageStr forKey:@"value"];
    
    [dict setValue:@[dictField,dictField1] forKey:@"filters"];
    
    [YMNetwork postWithUrl:url parameters:dict success:^(id  _Nonnull data) {
        NSString *status = [SDataUtil stringOf:data[@"status"]];
        if ([YMNetwork tuoTuSuccess:status]){
            NSDictionary *dict = [SDataUtil dictOf:data[@"result"]];
            NSDictionary *dictData = [[NSDictionary alloc] init];
            NSString *nameStr = @"";
            NSArray *arrJson = @[];
            if (dict != nil){
                NSArray *arrTemp = [SDataUtil arrayOf:dict[@"data"]];
                if (arrTemp.count > 0){
                    dictData = [SDataUtil dictOf:arrTemp.firstObject];
                    NSArray *arrItem = [SDataUtil arrayOf:dictData[@"tfilhnr91gkmekdd"]];
                    if (arrItem.count > 0){
                        NSDictionary *dictName = [SDataUtil dictOf:arrItem.firstObject];
                        nameStr = [SDataUtil stringOf:dictName[@"value"]];
                    }
                    NSString *jsonStr = [SDataUtil stringOf:dictData[@"e5tlqxpbrortyd6x"] valid:@""];
                    arrJson = [YMCheckService toArrayWithJsonStr:jsonStr];
                }
            }
            if (success){
                success(nameStr,arrJson);
            }
        }else{
            NSString *msg = [SDataUtil stringOf:data[@"err_msg"] valid:KLanguage(@"net_error_later_retry")];
            if (failure){
                failure(msg);
            }
        }
    } failure:^(NSError * _Nonnull error) {
        if (failure){
            failure(KLanguage(@"net_error_later_retry"));
        }
    }];
}

//试剂卡管理查询接口
+ (void)requestCheckDetectionCardDataWithIdStr:(NSString *)idStr
                                      cardCode:(NSString *)cardCode
                                       success:(void(^)(BOOL canUse, NSDictionary *dictData))success
                                       failure:(void(^)(NSString *message))failure{

    NSString *url = [KTuoToUrl stringByAppendingString:@"/appsheets/sheetapi/getFormList"];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setValue:@"13aa4626c7014c1b93136c038019db8c" forKey:@"applicationid"];
    [dict setValue:@"611558d23f5e45eaafc974284e079206" forKey:@"formheaduuid"];
    [dict setValue:@0 forKey:@"needpage"];
    [dict setValue:@1 forKey:@"page"];
    [dict setValue:@20 forKey:@"pagesize"];
    
    NSMutableDictionary *dictField1 = [[NSMutableDictionary alloc] init];
    NSString *languageStr = [YMLanguageUtil isEnglish] ? @"3cc86818-6f66-4409-93ac-78c0ef70ab0f":@"d5a51385-3eeb-49fb-b6b7-64e77f2c5e92";
    [dictField1 setValue:@"cpqjuttpyfgvakmf" forKey:@"key"];
    [dictField1 setValue:languageStr forKey:@"value"];
    
//    NSMutableDictionary *dictField1 = [[NSMutableDictionary alloc] init];
//    [dictField1 setValue:@"lcq6twxkuyytwpkc" forKey:@"key"];
//    [dictField1 setValue:[SDataUtil stringOf:idStr valid:@""] forKey:@"value"];
    
    NSMutableDictionary *dictField2 = [[NSMutableDictionary alloc] init];
    [dictField2 setValue:@"tkynuu9ecdit0llv" forKey:@"key"];
    [dictField2 setValue:[SDataUtil stringOf:cardCode valid:@""] forKey:@"value"];
    
    [dict setValue:@[dictField1,dictField2] forKey:@"filters"];
    
    [YMNetwork postWithUrl:url parameters:dict success:^(id  _Nonnull data) {
        NSString *status = [SDataUtil stringOf:data[@"status"]];
        if ([YMNetwork tuoTuSuccess:status]){
            NSDictionary *dict = [SDataUtil dictOf:data[@"result"]];
            NSDictionary *dictData = [[NSDictionary alloc] init];
            BOOL canUse = NO;
            if (dict != nil){
                NSArray *arrTemp = [SDataUtil arrayOf:dict[@"data"]];
                if (arrTemp.count > 0){
                    dictData = [SDataUtil dictOf:arrTemp.firstObject];
                    canUse = YES;
                }
            }
            if (success){
                success(canUse,dictData);
            }
        }else{
            NSString *msg = [SDataUtil stringOf:data[@"err_msg"] valid:KLanguage(@"net_error_later_retry")];
            if (failure){
                failure(msg);
            }
        }
    } failure:^(NSError * _Nonnull error) {
        if (failure){
            failure(KLanguage(@"net_error_later_retry"));
        }
    }];
}

//添加检测记录
+ (void)requestCheckAddDetectionRecordWithDict:(NSDictionary *)dictParam
                                       success:(void(^)(NSString *detectionId))success
                                       failure:(void(^)(NSString *message))failure{

    NSString *url = [kBaseUrl stringByAppendingString:@"/app/user/addDetectionRecord"];
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:dictParam];
    [dict setValue:[YMMyModel shared].tuoTuToken forKey:@"tuoTuToken"];
    [dict setValue:[YMMyModel shared].userId forKey:@"tuoTuUserId"];
    [dict setValue:[YMDeviceTools phoneModel] forKey:@"phoneModel"];
    
    [YMNetwork postWithUrl:url parameters:dict success:^(id  _Nonnull data) {
        NSInteger code = [SDataUtil intOf:data[@"code"]];
        if (code == 200){
            NSDictionary *dict = [SDataUtil dictOf:data[@"data"]];
            if (success){
                success([SDataUtil stringOf:dict[@"detectionId"] valid:@""]);
            }
        }else{
            NSString *msg = [SDataUtil stringOf:data[@"msg"] valid:KLanguage(@"net_error_later_retry")];
            if (failure){
                failure(msg);
            }
        }
    } failure:^(NSError * _Nonnull error) {
        if (failure){
            failure(KLanguage(@"net_error_later_retry"));
        }
    }];
}

+ (NSArray *)toArrayWithJsonStr:(NSString *)jsonStr
{
    if ([jsonStr isKindOfClass:[NSArray class]]) {
        return (NSArray *)jsonStr;
    }
    if (jsonStr == nil) {
        return nil;
    }
    NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSArray *resultArr = [NSJSONSerialization JSONObjectWithData:jsonData
                                                   options:NSJSONReadingMutableContainers
                                                    error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return resultArr;
}



@end
